﻿using DevExpress.XtraEditors;
using Shopfloormanagement;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace Shopfloormanagement
{
    public partial class Fish : DevExpress.XtraEditors.XtraForm
    {
        public Fish()
        {
            InitializeComponent();
        }

        

        private void btnSave_Click(object sender, EventArgs e)
        {
            DataTable Fish = LSQL.SQL.ReturnDataTable2(SystemInfo.SQL, "A3_Step4", $"A3ReportNumberID = '{rootcause.A3number}'");
            if (Fish.Rows.Count > 0)
            {

                string content = $@"Fishbone = '{txtfish.Text}',Man1 = '{txtman1.Text}',Man2 = '{txtman2.Text}',Man3 = '{txtman3.Text}',Man4 = '{txtman4.Text}',Man5 = '{txtman5.Text}',
                    Machine1 = '{txtmachine1.Text}',Machine2 = '{txtmachine2.Text}',Machine3 = '{txtmachine3.Text}',Machine4 = '{txtmachine4.Text}',Machine5 = '{txtmachine5.Text}',
                    Material1 = '{txtmaterial1.Text}',Material2 = '{txtmaterial2.Text}',Material3 = '{txtmaterial3.Text}',Material4 = '{txtmaterial4.Text}',Material5 = '{txtmaterial5.Text}',
                    Method1 = '{txtmethod1.Text}',Method2 = '{txtmethod2.Text}',Method3 = '{txtmethod3.Text}',Method4 = '{txtmethod4.Text}',Method5 = '{txtmethod5.Text}',
                    Mileu1 = '{txtmileu1.Text}',Mileu2 = '{txtmileu2.Text}',Mileu3 = '{txtmileu3.Text}',Mileu4 = '{txtmileu4.Text}',Mileu5 = '{txtmileu5.Text}',
                    Measurement1 = '{txtmeasurement1.Text}',Measurement2 = '{txtmeasurement2.Text}',Measurement3 = '{txtmeasurement3.Text}',Measurement4 = '{txtmeasurement4.Text}',Measurement5 = '{txtmeasurement5.Text}'";
                bool resstepfish = LSQL.SQL.Update(SystemInfo.SQL, "A3_Step4", content, $"A3ReportNumberID = '{rootcause.A3number}'");
                bool reslist = LSQL.SQL.Update(SystemInfo.SQL, "A3_List", $"Step4 = 1,A3ProblemStatement='{txtfish.Text}'", $"ID = '{rootcause.A3number}'");
                bool reslistA5 = LSQL.SQL.Delete(SystemInfo.SQL, "A3_Step5", $"A3ReportNumberID = '{rootcause.A3number}'");
                if (resstepfish == true && reslist == true && reslistA5 == true)
                {

                    MessageBox.Show("更新成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Fish_Load(sender, e);
                    
                }
                else
                {
                    MessageBox.Show("更新失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);

                }

            }

            else
            {
                string colunm = "[A3ReportNumberID],[Fishbone],[Man1],[Man2],[Man3],[Man4],[Man5]," +
                    "[Machine1],[Machine2],[Machine3],[Machine4],[Machine5]," +
                    "[Material1],[Material2],[Material3],[Material4],[Material5]," +
                    "[Method1],[Method2],[Method3],[Method4],[Method5]," +
                    "[Mileu1],[Mileu2],[Mileu3],[Mileu4],[Mileu5]," +
                    "[Measurement1],[Measurement2],[Measurement3],[Measurement4],[Measurement5]";
                string content = $@"'{rootcause.A3number}','{txtfish.Text}','{txtman1.Text}','{txtman2.Text}','{txtman3.Text}','{txtman4.Text}','{txtman5.Text}',
                    '{txtmachine1.Text}','{txtmachine2.Text}','{txtmachine3.Text}','{txtmachine4.Text}','{txtmachine5.Text}',
                    '{txtmaterial1.Text}','{txtmaterial2.Text}','{txtmaterial3.Text}','{txtmaterial4.Text}','{txtmaterial5.Text}',
                    '{txtmethod1.Text}','{txtmethod2.Text}','{txtmethod3.Text}','{txtmethod4.Text}','{txtmethod5.Text}',
                    '{txtmileu1.Text}','{txtmileu2.Text}','{txtmileu3.Text}','{txtmileu4.Text}','{txtmileu5.Text}',
                    '{txtmeasurement1.Text}','{txtmeasurement2.Text}','{txtmeasurement3.Text}','{txtmeasurement4.Text}','{txtmeasurement5.Text}'";
                int res = LSQL.SQL.Insert(SystemInfo.SQL, "A3_Step4", colunm, content);
                if (res == 1)
                {
                    bool reslist = LSQL.SQL.Update(SystemInfo.SQL, "A3_List", "Step4 = 1", $"ID = '{rootcause.A3number}'");
                    if (reslist == true)
                    {
                        MessageBox.Show("保存成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        Fish_Load(sender, e);
                        
                    }
                }
                else
                {
                    MessageBox.Show("保存失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);

                }

            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
            Step4 frm = new Step4();
            frm.Show();
        }
        private void Loading()//加载文字
        {
            DataTable Fishbone = LSQL.SQL.ReturnDataTable2(SystemInfo.SQL, "A3_List", $"ID = '{rootcause.A3number}'");
            txtfish.Text = Fishbone.Rows[0][4].ToString();
            DataTable Fish = LSQL.SQL.ReturnDataTable2(SystemInfo.SQL, "A3_Step4", $"A3ReportNumberID = '{rootcause.A3number}'");
            if (Fish.Rows.Count == 0) return; // 如果没有数据，直接返回
            DataRow row = Fish.Rows[0]; // 取第 0 行数据
            // 加载Man
            for (int i = 1; i <= 5; i++)
            {
                string columnName = $"Man{i}";

                // 确保列存在
                if (!Fish.Columns.Contains(columnName)) continue;

                // 获取 TextEdit 控件
                Control[] controls = this.Controls.Find($"txtman{i}", true);
                if (controls.Length > 0 && controls[0] is DevExpress.XtraEditors.TextEdit textEdit)
                {
                    // 赋值，忽略 NULL
                    textEdit.Text = row[columnName] != DBNull.Value ? row[columnName].ToString() : string.Empty;
                }
            }
            //加载Machine
            for (int i = 1; i <= 5; i++)
            {
                string columnName = $"Machine{i}";

                // 确保列存在
                if (!Fish.Columns.Contains(columnName)) continue;

                // 获取 TextEdit 控件
                Control[] controls = this.Controls.Find($"txtmachine{i}", true);
                if (controls.Length > 0 && controls[0] is DevExpress.XtraEditors.TextEdit textEdit)
                {
                    // 赋值，忽略 NULL
                    textEdit.Text = row[columnName] != DBNull.Value ? row[columnName].ToString() : string.Empty;
                }
            }
            //加载Material
            for (int i = 1; i <= 5; i++)
            {
                string columnName = $"Material{i}";

                // 确保列存在
                if (!Fish.Columns.Contains(columnName)) continue;

                // 获取 TextEdit 控件
                Control[] controls = this.Controls.Find($"txtmaterial{i}", true);
                if (controls.Length > 0 && controls[0] is DevExpress.XtraEditors.TextEdit textEdit)
                {
                    // 赋值，忽略 NULL
                    textEdit.Text = row[columnName] != DBNull.Value ? row[columnName].ToString() : string.Empty;
                }
            }
            //加载Method
            for (int i = 1; i <= 5; i++)
            {
                string columnName = $"Method{i}";

                // 确保列存在
                if (!Fish.Columns.Contains(columnName)) continue;

                // 获取 TextEdit 控件
                Control[] controls = this.Controls.Find($"txtmethod{i}", true);
                if (controls.Length > 0 && controls[0] is DevExpress.XtraEditors.TextEdit textEdit)
                {
                    // 赋值，忽略 NULL
                    textEdit.Text = row[columnName] != DBNull.Value ? row[columnName].ToString() : string.Empty;
                }
            }
            //加载Mileu
            for (int i = 1; i <= 5; i++)
            {
                string columnName = $"Mileu{i}";

                // 确保列存在
                if (!Fish.Columns.Contains(columnName)) continue;

                // 获取 TextEdit 控件
                Control[] controls = this.Controls.Find($"txtmileu{i}", true);
                if (controls.Length > 0 && controls[0] is DevExpress.XtraEditors.TextEdit textEdit)
                {
                    // 赋值，忽略 NULL
                    textEdit.Text = row[columnName] != DBNull.Value ? row[columnName].ToString() : string.Empty;
                }
            }
            //加载Measurement
            for (int i = 1; i <= 5; i++)
            {
                string columnName = $"Measurement{i}";

                // 确保列存在
                if (!Fish.Columns.Contains(columnName)) continue;

                // 获取 TextEdit 控件
                Control[] controls = this.Controls.Find($"txtmeasurement{i}", true);
                if (controls.Length > 0 && controls[0] is DevExpress.XtraEditors.TextEdit textEdit)
                {
                    // 赋值，忽略 NULL
                    textEdit.Text = row[columnName] != DBNull.Value ? row[columnName].ToString() : string.Empty;
                }
            }
        }

        private void Fish_Load(object sender, EventArgs e)
        {
            Loading();
        }
    }
}